<?php 

namespace Admin\Controller;

class NewBlogCatController extends BaseController{
    // 获取博客栏目列表
    public function index(){
        
        
        $blog = M('cat');
        
        // 获取栏目数据
        $list = $blog->order('sort desc,cat_id desc')->select();
        $count = $blog->count();
        
        // 拼接栏目分类
        $cat_str = '';
        foreach ($list as $v){
            $cat_str .= '{ id:'.$v['cat_id'].', pId:'.$v['pid'].', name:"'.$v['cat_name'].'",src:"'.U('NewBlogCat/add',array('id'=>$v['cat_id'])).'"},';
        }
        
        // 传递数据
        $this->assign('cat_str',$cat_str);
        $this->assign('count',$count);
        
        $this->display('NewBlogCat/blogcat');
    }
    
    
    // 添加或修改数据
    public function add(){
        
        $blog = M('cat');
        
        // 获取栏目数据
        $list = threeData($blog->order('sort desc,cat_id desc')->select());
        $this->assign('list',$list);
        
        
        if (IS_POST){
            
            // 获取表单数据

            $data = I('post.');
            $blog = M('cat');
            $is_add = I('is_add');
            
            if (empty($data['cat_name'])){
                $this->error('栏目名称不能为空',U('add'));die;
            }
            
            if (I('is_show')==''){
                $this->error('请选择是否显示',U());
            }
            
            if ($is_add == 1){ //添加分类
                if (!empty(M('cat')->where('cat_name="'.I('cat_name').'"')->find())){
                    $this->error('分类名称已经存在',U('add'));die;
                }
                unset($data['id']);
                unset($data['is_add']);
                $res = $blog->data($data)->add();
            }else { // 修改分类
                
                if (I('pid') == I('id')){
                    $this->error('不能已自身类当做父类',U());
                }
                if (empty(M('cat')->where('cat_name="'.I('cat_name').'"')->find())){
                    $this->error('分类不存在',U());
                }
                
                $blog->pid = I('pid');
                $blog->cat_name = $data['cat_name'];
                $blog->sort = $data['sort'];
                $blog->is_show = $data['is_show'];
                $res = $blog->where('cat_id='.$data['id'])->save();
                if ($res == 0){
                    
                    $this->error('数据未修改',U());die;
                }
            }
            
            if (!$res){
                
                $this->error('添加数据失败！',U());
            }else {
                $this->show('<script>alert("成功");parent.location.reload();</script>');
            }
        }elseif(!empty(I('get.id'))){
            
            
            // 获取栏目数据
            $blog = M('cat');
            $res = $blog->where('cat_id='.I('get.id'))->find();
            
            $this->assign('res',$res);
            
            $this->display('NewBlogCat/cat_add');
        }else{
            
            $this->display('NewBlogCat/cat_add');
        }
    }
    
    // 删除数据
    public function delete(){
        
        $data = M('cat')->select();
        $subData = threeData($data,I('post.id'));
        $cat_id = I('post.id');
        
        // 删除数据
        $res = M('cat')->where('cat_id='.$cat_id)->delete();
        
        if ($res){
            // 删除栏目下面的所有博客
            M('blog')->where(array('cat_id'=>$cat_id))->delete();
            foreach ($subData as $v){
                M('cat')->where('cat_id='.$v['cat_id'])->delete();
                M('blog')->where('cat_id='.$cat_id)->delete();
            }
            echo 1;die;
        }else {
            echo 0;die;
        }
        
    }
    
}

?>